package org.argeproje.resim.proc.math;

import org.argeproje.resim.proc.ConnectionRule;
import org.argeproje.resim.proc.Processor;
import org.argeproje.resim.proc.data.ConstDA;
import org.argeproje.resim.proc.data.Data;
import org.argeproje.resim.proc.data.ImageDA;


public class ArithmeticPR extends MathPR {
   protected int _opType;
	protected void setConnectionRules(ConnectionRule connRule){
		connRule.addInType(ImageDA.class, 0);
		connRule.addInType(ConstDA.class, 0);
		connRule.addInType(ImageDA.class, 1);
		connRule.addInType(ConstDA.class, 1);
		connRule.addOutType(ImageDA.class);
	}
   public void setParameters(int opType){
   	  setParamOpType(opType);
   }
   public void setParamOpType(int opType){
   	_opType = opType;
   }
   public int getParamOpType(){
   	return _opType;
   }
   public Data process() {
   	Processor pr = getProcessor();
    setOutput(pr.process());
    return getOutput();
   }
   private Processor getProcessor() {
	boolean constOp = false;
	boolean imageOp = false;
	Data input0 = getInput(0);
	Data input1 = getInput(1);
	if ( (input0.isOfType(Data.CONST) && input1.isOfType(Data.IMAGE))
			|| (input0.isOfType(Data.IMAGE) && input1.isOfType(Data.CONST)) ) {
		constOp = true;
	}
	if (input0.isOfType(Data.IMAGE) && input1.isOfType(Data.IMAGE)){
		imageOp = true;
	}
	if (input0.isOfType(Data.COMPLEX_IMAGE) && input1.isOfType(Data.COMPLEX_IMAGE)){
		imageOp = true;
	}
	if(constOp){
		ConstArithmeticPR pr = new ConstArithmeticPR();
		pr.setParameters(getParamOpType());
		ConstDA constDA = null;
		ImageDA img = null;
		if(input0.isOfType(Data.CONST) ){
			constDA = (ConstDA)input0;
			img = (ImageDA)input1;
			if(getParamOpType() == MathPR.SUB){
			  pr.setParamOpType(MathPR.SUB_FROM);
			}
			else if(getParamOpType() == MathPR.DIV){
			  pr.setParamOpType(MathPR.DIV_FROM);
			}
		}
		else{
			constDA = (ConstDA)input1;
			img = (ImageDA)input0;
		}
	    Data[] inputs = new Data[2];
	    inputs[0]= img;
	    inputs[1] = constDA;
	    pr.setInputs(inputs);
	    return pr;
	}
	else if(imageOp){
		ImageArithmeticPR pr = new ImageArithmeticPR();
		pr.setParameters(getParamOpType());
		pr.setInputs(getInputs());
		if(input0.isOfType(Data.COMPLEX_IMAGE) 
				|| input1.isOfType(Data.COMPLEX_IMAGE)){
			if(getParamOpType() == MathPR.MUL){
				  pr.setParamOpType(MathPR.MUL_COMPLEX);
			}
			else if(getParamOpType() == MathPR.DIV){
				  pr.setParamOpType(MathPR.DIV_COMPLEX);
			}
			else{
				pr = null;
				return pr;
			}
		}
		return pr;
	}
	return null;
}
}
